package com.pb.infra.dynamicSetting.dynamTableColumnCfg.runnable;

import com.pb.infra.dynamicSetting.dynamTableColumnCfg.entity.MaFrDynamTableColumnCfg;
import com.pb.infra.dynamicSetting.dynamTableColumnCfg.mapper.MaFrDynamTableColumnCfgMapper;
import com.pb.infra.oth.util.GetBeanUtil;
import com.pb.infra.oth.util.StringUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;


public class DynTabColDataRunnable implements Runnable {

    private CountDownLatch latch;


    private List<MaFrDynamTableColumnCfg> dataList;

    private MaFrDynamTableColumnCfgMapper maFrDynamTableColumnCfgMapper;


    public DynTabColDataRunnable( CountDownLatch latch, List<MaFrDynamTableColumnCfg> dataList) {
        this.latch = latch;

        this.dataList = dataList;

        this.maFrDynamTableColumnCfgMapper = GetBeanUtil.getApplicationContext().getBean(MaFrDynamTableColumnCfgMapper.class);
    }

    @Override
    @Transactional
    public synchronized void run() {
        dataList.forEach(item -> {
            if (!StringUtil.isEmpty(item.getProp())) {
                item.setProp(item.getProp().toUpperCase(Locale.ENGLISH));
            }
            this.maFrDynamTableColumnCfgMapper.insert(item);
        });
        latch.countDown();
    }
}

